import Vue from 'vue'
// 引入的用户信息对象
import User from '../../utils/user'
import VueRouter from 'vue-router'
import Index from '../views/Index.vue'
import Case from '../views/Case.vue'
import Service from '../views/Service.vue'
import Helper from '../views/Helper.vue'
import Create from '../views/Create.vue'
import Login from '../views/Login.vue'
import Manage from '../views/Manage.vue'
import Items from '../views/Items.vue'
import Level from '../views/Level.vue'
import Invate from '../views/Invate.vue'
import Userinfo from '../views/Userinfo.vue'
import Editpass from '../views/Editpass.vue'
import Logs from '../views/Logs.vue'
import Pay from '../views/Pay.vue'
import VoteData from '../views/VoteData.vue'
import Account from '../views/Account.vue'
import DrawCashLog from '../views/DrawCashLog.vue'
import Reply from '../views/Reply.vue'
import Address from '../views/Address.vue'
import Invoice from '../views/Invoice.vue'
import InvoiceRecord from '../views/InvoiceRecord.vue'
// 重写路由push方法
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)
// 路由表
const routes = [
  {
    path: '/',
    name: '首页',
    component: Index,
    meta: {
      title: '首页'
    }
  },
  {
    path: '/Case',
    name: '案例',
    component: Case
  },
  {
    path: '/Service',
    name: '服务',
    component: Service,
    meta: {
      title:'服务'
    }
  },
  {
    path: '/Helper',
    name: '帮助',
    component: Helper
  },
  {
    path: '/Create',
    name: '创建',
    component: Create,
    meta : {
      title: '创建活动',
      requireLogin: true
    }
  },
  {
    path: '/Login',
    name: '登录',
    component: Login,
    meta: {
      title:'登录'
    }
  },
  {
    path: '/Manage',
    name: '管理',
    component: Manage,
    meta: {
      title:'管理活动',
      requireLogin: true
    }
  },
  {
    path: '/Items',
    name: '选手',
    component: Items,
    meta: {
      title: '选手列表',
      requireLogin: true
    }
  },
  {
    path: '/Level',
    name: '升级',
    component: Level,
    meta: {
      title: '会员升级',
      requireLogin: true
    }
  },
  {
    path: '/Invate',
    name: '邀请',
    component: Invate,
    meta: {
      title:'邀请',
      requireLogin: true
    }
  },
  {
    path: '/Userinfo',
    name: '个人信息',
    component: Userinfo,
    meta: {
      requireLogin: true,
      title:'个人信息'
    }
  },
  {
    path: '/Editpass',
    name: '修改密码',
    component: Editpass,
    meta: {
      requireLogin: true,
      title:'修改密码'
    }
  },
  {
    path: '/Logs',
    name: '消费记录',
    component: Logs,
    meta: {
      title:'消费记录',
      requireLogin: true
    }
  },
  {
    path: '/Pay',
    name: '充值',
    component: Pay,
    meta: {
      title: '帐户升级',
      requireLogin: true
    }
  },
  {
    path: '/VoteData',
    name: '投票统计',
    component: VoteData,
    meta: {
      title: '投票统计',
      requireLogin: true
    }
  },
  {
    path: '/Account',
    name: '收款信息',
    component: Account,
    meta: {
      title: '收款信息',
      requireLogin: true
    }
  },
  {
    path: '/DrawList',
    name: '提现记录',
    component: DrawCashLog,
    meta : {
      title: '提现记录',
      requireLogin: true
    }
  },
  {
    path: '/Reply',
    name: '评论管理',
    component: Reply,
    meta: {
      title: '评论管理',
      requireLogin: true
    }
  },
  {
    path: '/Address',
    name: '收件地址',
    component: Address,
    meta: {
      title: '收件地址',
      requireLogin: true
    }
  },
  {
    path: '/Invoice',
    name: '发票信息',
    component: Invoice,
    meta: {
      title: '发票信息',
      requireLogin: true
    }
  },
  {
    path: '/InvoiceRecord',
    name: '开票记录',
    component: InvoiceRecord,
    meta: {
      title: '开票记录',
      requireLogin: true
    }
  }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

// 全局导航钩子
router.beforeEach((to, from, next) => {
  if (to.meta.title) { // 路由发生变化修改页面title
    document.title = to.meta.title
  }

  if (to.meta.requireLogin) {
    if (!User.getToken() || !User.getInfo()) {
      next({
        path: '/Login',
        query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
      // next();
    } else {
      next();
    }
  } else {
    next()
  }
})

export default router
